#!/usr/bin/env bash
declare -A id_count
sql="select name from table1 order by id desc limit 3"
arr=$(mysql -uusername -ppassword -hlocalhost -P3306 -Ddatabase -Ne "${sql}")
for name in ${arr}
do
    echo "当前处理:${name}"
    value=$(mysql -uusername -ppassword -hlocalhost -P3306 -Ddatabase -Bse "select value from table2 where name = '${name}'")
    echo "value:${value}"
    value=$(echo ${value} | grep -Eo '[0-9]+')
    for id in ${value}
    do
        id_count[$id]=$((${id_count[$id]}+1))
    done
done
echo "key:${!id_count[*]}"
for id in ${!id_count[*]}
do
    echo "$id : ${id_count[$id]}"
done